package cn.edu.nju.ws.sview.views.data;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.views.View;

/**
 * 软件制品管理公共类
 * @author Saisai Gong
 *
 */
public class Management {
	public final static int VISIT_COUNT_VALID_INTERVAL;
	
	//control type
	public final static String CONTROL_ENTITY_TYPE = "entity";
	public final static String CONTROL_VIEW_TYPE = "view";
	public final static String CONTROL_DM_IN_COLLECTION_TYPE = "dm_in_collection";
	
	//visit status
	public final static int UNVISITED = 0;
	public final static int VISITED_COUNT_VALID = 1;
	public final static int VISITED_COUNT_INVALID = 2;

	
	static {
		Properties properties = new Properties();
		try {
			properties.load(View.class.getClassLoader().getResourceAsStream(
					"conf.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int interval = Integer.parseInt(properties
				.getProperty("visit_count_interval"));
		VISIT_COUNT_VALID_INTERVAL = interval * 60 * 60 * 1000;
		properties.clear();
	}
	
	/**
	 * 检查用户当天访问该软件制品的状态,判断是否可以计数
	 * @param userID.用户id
	 * @param control_type.控制类型
	 * @param etype.如果只有一套id体系，那么取值为0
	 * @param eid. 软件制品id
	 * @return.有效访问状态
	 */
	protected static int checkStatus(int userID, String control_type, int etype,
			int eid) {
		int status = UNVISITED;
		Connection con = DBConnectionFactory.getConnection();
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery(String
							.format("select date from user_day_visit_control where user_id=%d and member_type=%d and member_id=%d and control_type='%s'",
									userID, etype, eid,control_type));
			if (rs.next()) {
				Date date = rs.getTimestamp("date");
				Date now = new Date();
				long difference = now.getTime() - date.getTime();
				/* discriminate unsigned users */
				if (difference < VISIT_COUNT_VALID_INTERVAL) {
						status = VISITED_COUNT_INVALID;
				} else  {
						status = VISITED_COUNT_VALID;
				} 
			}
			rs.close();
			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return status;
	}
	
	/**
	 * 在user_day_visit_control表中加入最近访问信息
	 * 
	 * @return
	 */
	protected static void storeVisitInControl(int userID, String control_type,
			int etype, int eid) {
		Connection con = DBConnectionFactory.getConnection();
		try {
			PreparedStatement delstmt = con
					.prepareStatement("delete from user_day_visit_control where user_id=? and member_type=? and member_id=? and control_type=?");
			delstmt.setInt(1, userID);
			delstmt.setInt(2, etype);
			delstmt.setInt(3, eid);
			delstmt.setString(4, control_type);
			delstmt.executeUpdate();
			delstmt.close();
			PreparedStatement stmt = con
					.prepareStatement("insert into user_day_visit_control(user_id,member_type,member_id,control_type,date) values(?,?,?,?,NOW())");
			stmt.setInt(1, userID);
			stmt.setInt(2, etype);
			stmt.setInt(3, eid);
			stmt.setString(4, control_type);
			stmt.executeUpdate();
			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
